using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;

namespace ControlPoint.ControlAPI
{
	public class TimerButton : Device, IDeviceButton
	{
        private Timer timer;

        public TimerButton(TimerController controller, SettingsList settings)
            : base(controller, settings) {


                this.Settings.SettingChanged += OnSettingChanged;

                this.Settings.SetSetting("type", this.ToString());
                if (!this.Settings.ContainsSetting("hour")) {
                    Console.WriteLine("Missing hour");
                    this.Settings.SetSetting("hour", 10);
                }
                if (!this.Settings.ContainsSetting("minute")) {
                    Console.WriteLine("Missing minute");
                    this.Settings.SetSetting("minute", 10);
                } else {
                    SetTimer();
                }

                if (!this.Settings.ContainsSetting("timerId")) {
                    Console.WriteLine("Missing timeId");
                    this.Settings.SetSetting("timerId", controller.GetNextAvailableId());
                }
                if (!this.Settings.ContainsSetting("name")) {
                    Console.WriteLine("Missing name");
                    this.Settings.SetSetting("name", "Timer: " + ((int)this.Settings.GetSetting("timerId").Value).ToString() );
                }
                Console.WriteLine("done name");



            this._id = this._controller.ID + ":" + ((int)this.Settings.GetSetting("timerId").Value).ToString();
        }

        private void OnSettingChanged(Object sender, SettingChangedEventArgs e) {
            Console.WriteLine(this._id + ": Changed Setting: " + e.Setting.Name);
            if (e.Setting.Name == "hour" || e.Setting.Name == "minute") {
                SetTimer();
            }
        }

        private void SetTimer() {
            if (this.Settings.ContainsSetting("hour") && this.Settings.ContainsSetting("minute")) {
                DateTime now = DateTime.Now;
                DateTime todayInstance = now.Date.AddHours((int)this.Settings.GetSetting("hour").Value).AddMinutes((int)this.Settings.GetSetting("minute").Value);
                DateTime nextInstance = now <= todayInstance ? todayInstance : todayInstance.AddDays(1);

                this.timer = new Timer(OnTime, null, nextInstance - now, TimeSpan.FromHours(24));
                Console.WriteLine("Time set:" + this.timer.ToString() + " - " + nextInstance.ToShortTimeString());
            }
        }

        private void OnTime(Object state) {
            Console.WriteLine(this._id + ": OnTime");
            if (Press != null) {
                Press(this,"Press", new ActionValue());
            }
        }

		public void RegisterAction(ActionFiredDelegate actionProcessor)
		{
			Console.WriteLine("Action Registered");
            Press += actionProcessor;
		}

		public event ActionFiredDelegate DoublePress;

		public event ActionFiredDelegate LongPress;

		public event ActionFiredDelegate Press;

        public override SettingsList Settings {
            get {
                return base.Settings;
            }
        }
	}
}
